package com.czk.zookeeper.curator.create;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/7 20:50
 */
public class CuratorCreateTest {
    String IP = "192.168.25.133:2181,192.168.25.133:2182,192.168.25.133:2183";
    CuratorFramework client;

    @Before
    public void before() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.builder()
                .connectString(IP)
                .sessionTimeoutMs(50000)
                .retryPolicy(retryPolicy)
                .namespace("create")
                .build();
        client.start();
    }

    @After
    public void after() {
        client.close();
    }

    @Test
    public void create() throws Exception {
        // 新增节点
        client.create()
                // 持久化节点
                .withMode(CreateMode.PERSISTENT)
                // 权限列表
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                // arg1 节点名称
                // arg2 节点数据
                .forPath("/node10", "node10".getBytes());
        System.out.println("结束");
    }

    /**
     * 自定义权限列表
     */
    @Test
    public void test2() throws Exception {
        // 自定义权限列表
        List<ACL> list = new ArrayList<>(10);
        // 授权模式
        Id id = new Id("ip", "192.168.25.133");
        list.add(new ACL(ZooDefs.Perms.ALL, id));
        client.create().withMode(CreateMode.PERSISTENT).withACL(list).forPath("/node11", "node11".getBytes());
        //
        System.out.println("结束");
    }

    /**
     * 对创建节点
     *
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
        // 递归创建节点树
        client.create()
                // 支持递归创建节点
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath("/node12/node13", "node13".getBytes());
    }

    /**
     * 异步方式创建节点
     *
     * @throws Exception
     */
    @Test
    public void test4() throws Exception {
        // 异步方式创建节点
        client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                // 异步回到接口
                .inBackground(new BackgroundCallback() {
                    @Override
                    public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                        // 节点的路径
                        System.out.println(curatorEvent.getPath());
                        // 事件的类型
                        System.out.println(curatorEvent.getType());
                    }
                })
                .forPath("/node15", "node15".getBytes());
        Thread.sleep(10 * 1000);
        System.out.println("结束");
    }
}
